package org.dromara.book.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.book.domain.bo.SzlyBaseBatchBo;
import org.dromara.book.domain.vo.SzlyBaseBatchVo;
import org.dromara.book.domain.SzlyBaseBatch;
import org.dromara.book.mapper.SzlyBaseBatchMapper;
import org.dromara.book.service.ISzlyBaseBatchService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 批次管理Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-19
 */
@RequiredArgsConstructor
@Service
public class SzlyBaseBatchServiceImpl implements ISzlyBaseBatchService {

    private final SzlyBaseBatchMapper baseMapper;

    /**
     * 查询批次管理
     */
    @Override
    public SzlyBaseBatchVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询批次管理列表
     */
    @Override
    public TableDataInfo<SzlyBaseBatchVo> queryPageList(SzlyBaseBatchBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SzlyBaseBatch> lqw = buildQueryWrapper(bo);
        Page<SzlyBaseBatchVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询批次管理列表
     */
    @Override
    public List<SzlyBaseBatchVo> queryList(SzlyBaseBatchBo bo) {
        LambdaQueryWrapper<SzlyBaseBatch> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SzlyBaseBatch> buildQueryWrapper(SzlyBaseBatchBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SzlyBaseBatch> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), SzlyBaseBatch::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getFundsSource()), SzlyBaseBatch::getFundsSource, bo.getFundsSource());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplier()), SzlyBaseBatch::getSupplier, bo.getSupplier());
        lqw.eq(StringUtils.isNotBlank(bo.getDiscount()), SzlyBaseBatch::getDiscount, bo.getDiscount());
        lqw.eq(StringUtils.isNotBlank(bo.getReceiptNumber()), SzlyBaseBatch::getReceiptNumber, bo.getReceiptNumber());
        return lqw;
    }

    /**
     * 新增批次管理
     */
    @Override
    public Boolean insertByBo(SzlyBaseBatchBo bo) {
        SzlyBaseBatch add = MapstructUtils.convert(bo, SzlyBaseBatch.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改批次管理
     */
    @Override
    public Boolean updateByBo(SzlyBaseBatchBo bo) {
        SzlyBaseBatch update = MapstructUtils.convert(bo, SzlyBaseBatch.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SzlyBaseBatch entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除批次管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
